package top.wshape1.easyscan.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.nfc.Tag;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.constraint.PackageEnum;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-05-17
 */


public class GoToUtil {

    private static final String TAG = "GoToUtil";

    public static String getMethodName(PackageEnum packageEnum) {
        return "goTo" + packageEnum.getAppName();
    }

    public static void commonGoTo(EasyScanActivity context, String appName, String packageName, String activityPath) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setComponent(new ComponentName(packageName, activityPath));
            context.resultLauncher.launch(intent);
            context.startActivity(intent);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(context, "没有安装" + appName, Toast.LENGTH_LONG).show();
        }
    }

    @SuppressLint("WrongConstant")
    public static void goToWeChat(EasyScanActivity context) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
            intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
            intent.setFlags(335544320);
//            intent.setAction("android.intent.action.VIEW");
            context.resultLauncher.launch(intent);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(context, "没有安装微信", Toast.LENGTH_LONG).show();
        }
    }

    public static void goToQq(EasyScanActivity context) {
        commonGoTo(context, "QQ", "com.tencent.mobileqq", "com.tencent.qqlive.module.videoreport.scheme.SchemeRouterActivity");
    }

    public static void goToAlipay(EasyScanActivity context) {
        try {
            Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.resultLauncher.launch(intent);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(context, "没有安装支付宝", Toast.LENGTH_SHORT).show();
        }
    }

    public static void goToChaoXing(EasyScanActivity context) {
        commonGoTo(context, "学习通", "com.chaoxing.mobile", "com.chaoxing.mobile.activity.JumpActivity");
    }

    public static void goToUrl(EasyScanActivity context, String Url) {
        try {
            Uri uri = Uri.parse(Url);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            context.resultLauncher.launch(intent);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(context, "跳转浏览器失败", Toast.LENGTH_SHORT).show();
        }
    }

}
